/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.tousu.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.biz.modular.tousu.entity.Tousu;
import vip.xiaonuo.biz.modular.tousu.param.TousuAddParam;
import vip.xiaonuo.biz.modular.tousu.param.TousuEditParam;
import vip.xiaonuo.biz.modular.tousu.param.TousuIdParam;
import vip.xiaonuo.biz.modular.tousu.param.TousuPageParam;
import vip.xiaonuo.biz.modular.tousu.service.TousuService;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 投诉信息表控制器
 *
 * @author damionew
 * @date  2025/07/08 15:47
 */
@Tag(name = "投诉信息表控制器")
@RestController
@Validated
@Slf4j
public class TousuController {

    @Resource
    private TousuService tousuService;

    @Operation(summary = "获取首页数据")
    @GetMapping(value = "/biz/tousu/getIndexData", produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonResult<JSONObject> getIndexData(HttpServletResponse response) throws IOException {
        // 昨日大盘投诉量
        int lastDapanCount = tousuService.getLastDapanCount();
        float lastDapanRate= tousuService.getLastDapanRate();
        // 昨日投诉量
        int lastTousuCount = tousuService.getLastTousuCount();
        // 昨日投诉量同比
        float lastTousuRate = tousuService.getLastTousuRate();
        // 昨日归属投诉量
        int lastBelongTousuCount = tousuService.getLastBelongTousuCount();
        // 昨日归属投诉量同比
        float lastBelongTousuRate = tousuService.getLastBelongTousuRate();
        // 本月投诉量
        int thisMonthTousuCount = tousuService.getThisMonthTousuCount();
        // 本月投诉量同比
        float thisMonthTousuRate = tousuService.getThisMonthTousuRate();
        // 本季度投诉量
        int thisQuarterTousuCount = tousuService.getThisQuarterTousuCount();
        // 本季度投诉量同比
        float thisQuarterTousuRate = tousuService.getThisQuarterTousuRate();

        JSONObject object = new JSONObject();
        object.set("lastDapanFeedback", lastDapanCount);
        object.set("lastDapanFeedbackRate", lastDapanRate * 100);
        object.set("lastFeedback", lastTousuCount);
        object.set("lastFeedbackRate", lastTousuRate * 100);
        object.set("lastFeedbackBelong", lastBelongTousuCount);
        object.set("lastFeedbackBelongRate", lastBelongTousuRate * 100);
        object.set("thisMonthFeedback", thisMonthTousuCount);
        object.set("thisMonthFeedbackRate", thisMonthTousuRate * 100);
        object.set("thisQuarterFeedback", thisQuarterTousuCount);
        object.set("thisQuarterFeedbackRate", thisQuarterTousuRate * 100);
        log.info("获取首页数据:{}", object);
        return CommonResult.data(object);
    }

    @Operation(summary = "获取大盘line数据")
    @GetMapping(value = "/biz/tousu/getDapanData", produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonResult<JSONObject> getDapanData(String startDate, String endDate) throws IOException {
        List<Map<String, Integer>> lastDapanCount = tousuService.getDapanLineData(startDate, endDate);
        JSONObject object = new JSONObject();
        object.set("dapanData", lastDapanCount);
        log.info("获取首页数据:{}", object);
        return CommonResult.data(object);
    }

    @Operation(summary = "获取投诉line数据")
    @GetMapping(value = "/biz/tousu/getTousuData", produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonResult<JSONObject> getTousuData(String startDate, String endDate) throws IOException {
        List<Map<String, Integer>> lastDapanCount = tousuService.getTousuLineData(startDate, endDate);
        JSONObject object = new JSONObject();
        object.set("tousuData", lastDapanCount);
        log.info("获取首页数据:{}", object);
        return CommonResult.data(object);
    }

    @Operation(summary = "获取我的号码数据")
    @GetMapping(value = "/biz/tousu/getTousuNumberList", produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonResult<JSONObject> getTousuNumberList(String searchKey) throws IOException {
        List<Map<String, String>> tousuNumberList = tousuService.getTousuNumberList(searchKey);
        JSONObject object = new JSONObject();
        object.set("tousuNumberList", tousuNumberList);
        log.info("获取首页数据:{}", object);
        return CommonResult.data(object);
    }

    /**
     * 获取投诉信息表分页
     *
     * @author damionew
     * @date  2025/07/08 15:47
     */
    @Operation(summary = "获取投诉信息表分页")
    @GetMapping("/biz/tousu/page")
    public CommonResult<Page<Tousu>> page(TousuPageParam tousuPageParam) {
        return CommonResult.data(tousuService.page(tousuPageParam));
    }

    /**
     * 获取投诉信息表分页
     *
     * @author damionew
     * @date  2025/07/08 15:47
     */
    @Operation(summary = "获取投诉信息表分页")
    @GetMapping("/biz/tousu/getTousuList")
    public CommonResult<Page<Tousu>> getTousuList(TousuPageParam tousuPageParam) {
        return CommonResult.data(tousuService.page(tousuPageParam));
    }

    /**
     * 添加投诉信息表
     *
     * @author damionew
     * @date  2025/07/08 15:47
     */
    @Operation(summary = "添加投诉信息表")
    @CommonLog("添加投诉信息表")
    @PostMapping("/biz/tousu/add")
    public CommonResult<String> add(@RequestBody @Valid TousuAddParam tousuAddParam) {
        tousuService.add(tousuAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑投诉信息表
     *
     * @author damionew
     * @date  2025/07/08 15:47
     */
    @Operation(summary = "编辑投诉信息表")
    @CommonLog("编辑投诉信息表")
    @PostMapping("/biz/tousu/edit")
    public CommonResult<String> edit(@RequestBody @Valid TousuEditParam tousuEditParam) {
        tousuService.edit(tousuEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除投诉信息表
     *
     * @author damionew
     * @date  2025/07/08 15:47
     */
    @Operation(summary = "删除投诉信息表")
    @CommonLog("删除投诉信息表")
    @PostMapping("/biz/tousu/delete")
    public CommonResult<String> delete(@RequestParam("id") int id) {
        tousuService.delete(id);
        return CommonResult.ok();
    }

    /**
     * 获取投诉信息表详情
     *
     * @author damionew
     * @date  2025/07/08 15:47
     */
    @Operation(summary = "获取投诉信息表详情")
    @SaCheckPermission("/biz/tousu/detail")
    @GetMapping("/biz/tousu/detail")
    public CommonResult<Tousu> detail(@Valid TousuIdParam tousuIdParam) {
        return CommonResult.data(tousuService.detail(tousuIdParam));
    }

    /**
     * 下载投诉信息表导入模板
     *
     * @author damionew
     * @date  2025/07/08 15:47
     */
    @Operation(summary = "下载投诉信息表导入模板")
    @GetMapping(value = "/biz/tousu/downloadImportTemplate", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void downloadImportTemplate(HttpServletResponse response) throws IOException {
        tousuService.downloadImportTemplate(response);
    }

    /**
     * 导入投诉信息表
     *
     * @author damionew
     * @date  2025/07/08 15:47
     */
    @Operation(summary = "导入投诉信息表")
    @CommonLog("导入投诉信息表")
    @SaCheckPermission("/biz/tousu/importData")
    @PostMapping("/biz/tousu/importData")
    public CommonResult<JSONObject> importData(@RequestPart("file") MultipartFile file) {
        return CommonResult.data(tousuService.importData(file));
    }

    /**
     * 导出投诉信息表
     *
     * @author damionew
     * @date  2025/07/08 15:47
     */
    @Operation(summary = "导出投诉信息表")
    @SaCheckPermission("/biz/tousu/exportData")
    @PostMapping(value = "/biz/tousu/exportData", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportData(@RequestBody List<TousuIdParam> tousuIdParamList, HttpServletResponse response) throws IOException {
        tousuService.exportData(tousuIdParamList, response);
    }

    @PostMapping(value = "/biz/tousu/updateIssuedNumber")
    public CommonResult<Object> updateIssuedNumber(@RequestBody TousuEditParam tousuEditParam ){
        tousuService.updateIssuedNumber(tousuEditParam);
        return CommonResult.ok();
    }

    @PostMapping(value = "/biz/tousu/updateBelongCustomer")
    public CommonResult<Object> updateBelongCustomer(@RequestBody TousuEditParam tousuEditParam ){
        tousuService.updateBelongCustomer(tousuEditParam);
        return CommonResult.ok();
    }
    @PostMapping(value = "/biz/tousu/updateBelongArea")
    public CommonResult<Object> updateBelongArea(@RequestBody TousuEditParam tousuEditParam ){
        tousuService.updateBelongArea(tousuEditParam);
        return CommonResult.ok();
    }
    @PostMapping(value = "/biz/tousu/tousuUpdateForm")
    public CommonResult<Object> tousuUpdateForm(@RequestBody TousuEditParam tousuEditParam ){
        tousuService.tousuUpdateForm(tousuEditParam);
        return CommonResult.ok();
    }

}
